package Commer;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Count extends JFrame {
    private JPanel inputPanel;
    private List<JTextField> textFields = new ArrayList<>();


    public Count() {
        // 初始化框架
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(300, 200);
        this.setLayout(new BorderLayout());
        // 窗口大小不可改变
        this.setResizable(false);

        // 创建按钮面板
        JPanel buttonPanel = new JPanel();
        JButton createButton = new JButton("增加人数");
        JButton saveButton = new JButton("保存数据");
        createButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                createInputs();
            }
        });
        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                saveData();
            }
        });
        buttonPanel.add(createButton);
        buttonPanel.add(saveButton);

        // 添加按钮面板到框架
        this.add(buttonPanel, BorderLayout.NORTH);

        // 创建用于放置输入框的面板
        inputPanel = new JPanel();
        inputPanel.setLayout(new GridLayout(0, 1)); // 使用GridLayout布局管理器，以便输入框垂直排列
        this.add(inputPanel, BorderLayout.CENTER);

        // 显示框架
        this.setVisible(true);
    }

    private void createInputs() {
        // 弹出对话框询问用户需要多少个输入框
        String input = JOptionPane.showInputDialog(this, "请输入输入框的数量:");
        if (input == null || input.trim().isEmpty()) {
            return; // 用户取消或未输入任何内容
        }
        int numInputs = Integer.parseInt(input);

        // 清空之前的输入框
        inputPanel.removeAll();
        textFields.clear();

        // 根据用户输入的数量创建相应的输入框
        for (int i = 0; i < numInputs+1; i++) {
            JTextField textField = new JTextField();
            if(i==0){
                textField.setBackground(Color.lightGray);
            }
            inputPanel.add(textField);
            textFields.add(textField);
        }


        // 重新布局以显示新的输入框
        inputPanel.revalidate();
        inputPanel.repaint();
    }

    private void saveData() {
        if (textFields.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请先输入数据", "错误", JOptionPane.ERROR_MESSAGE);
            return;
        }

        // 遍历所有输入框，获取其值
        List<String> values = new ArrayList<>();
        for (JTextField textField : textFields) {
            String value = textField.getText().trim();
            if (value.isEmpty()) {
                JOptionPane.showMessageDialog(this, "请输入所有值", "错误", JOptionPane.ERROR_MESSAGE);
                return;
            }
            values.add(value);
        }

        try (FileWriter writer = new FileWriter("Test.txt", true)) {
            for (String value : values) {
                int intValue = Integer.parseInt(value);
                writer.write(intValue + "\n");
            }
            JOptionPane.showMessageDialog(this, "保存成功", "成功", JOptionPane.INFORMATION_MESSAGE);
            // 点击成功后，关闭窗口
            this.dispose();
        } catch (IOException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this, "保存失败: " + e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(this, "请输入有效的整数", "错误", JOptionPane.ERROR_MESSAGE);
        }
    }
}
